package shiftCipher;

import java.util.*;

public class ShiftCipher {
	
	static Scanner in = new Scanner(System.in);
	
	public static void main(String[] args) {
		String input;
		boolean quit = false;
		System.out.print("Welcome to Shift Cipher!");
		while(!quit) {
			System.out.print("What would you like to do?\n" +
					"\t1) Encode\n\t2) Decode\n\t3) Exit\nChoice: ");
			input = in.nextLine();
			if(input.contains("1"))
				encode();
			else if(input.contains("2"))
				decode();
			else if(input.contains("3"))
				quit = true;
			else
				System.out.println("I'm sorry that is not recognized as valid input.\nPlease try again\n");
		}
		System.out.println("Goodbye!\n");
	}
	
	public static void encode(){
		Scanner thisIn = new Scanner(System.in);
		System.out.print("Please enter a string you would like to encode\nInput String: ");
		String inputString = in.nextLine();
		inputString = inputString.toUpperCase();
		System.out.print("\nPlease enter an integer for the shift value\nShift Value: ");
		int shift = thisIn.nextInt();
		shift = shift % 26;
		String encodedString = "";
		char[] inputArray = inputString.toCharArray();
		int size = inputArray.length;
		if(shift > 0) {
			for(int i = 0; i < size; i++) {
				int ascii = (int)inputArray[i];
				if(ascii > 64 && ascii <= 90 - shift) {
					ascii = ascii + shift;
					encodedString = encodedString + (char)ascii;
				}
				else if(ascii > 90 - shift && ascii < 91) {
					ascii = 64 + ((ascii + shift) % 90);
					encodedString = encodedString + (char)ascii;
				}
			}
		}
		else {
			for(int i = 0; i < size; i++) {
				int ascii = (int)inputArray[i];
				if(ascii + shift > 64 && ascii + shift <= 90) {
					ascii = ascii + shift;
					encodedString = encodedString + (char)ascii;
				}
				else if(ascii > 64 && ascii + shift <= 64) {
					ascii = 90 - (64 - (ascii + shift));
					encodedString = encodedString + (char)ascii;
				}
			}
		}
		System.out.print("\nThe Encoded String: " + encodedString + "\n");
	}
	
	public static void decode() {
		Scanner thisIn = new Scanner(System.in);
		System.out.print("Please enter a string you would like to decode\nInput String: ");
		String inputString = in.nextLine();
		inputString = inputString.toUpperCase();
		System.out.print("\nPlease enter the integer used for the shift value\nShift Value: ");
		int shift = thisIn.nextInt();
		shift = -1*(shift % 26);
		String decodedString = "";
		char[] inputArray = inputString.toCharArray();
		int size = inputArray.length;
		if(shift < 0) {
			for(int i = 0; i < size; i++) {
				int ascii = (int)inputArray[i];
				if(ascii + shift > 64 && ascii + shift <= 90) {
					ascii = ascii + shift;
					decodedString = decodedString + (char)ascii;
				}
				else if(ascii > 64 && ascii + shift <= 64) {
					ascii = 90 - (64 - (ascii + shift));
					decodedString = decodedString + (char)ascii;
				}
			}
		}
		else if(shift > 0) {
			for(int i = 0; i < size; i++) {
				int ascii = (int)inputArray[i];
				if(ascii > 64 && ascii <= 90 - shift) {
					ascii = ascii + shift;
					decodedString = decodedString + (char)ascii;
				}
				else if(ascii > 90 - shift && ascii < 91) {
					ascii = 64 + ((ascii + shift) % 90);
					decodedString = decodedString + (char)ascii;
				}
			}
		}
		System.out.print("\nThe Decoded String: " + decodedString + "\n");
	}
}
